package com.qianfeng.goods.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONArray;
import com.qianfeng.goods.bean.Cart;
import com.qianfeng.goods.bean.Goods;
import com.qianfeng.goods.bean.Order;
import com.qianfeng.goods.bean.TypeBean;
import com.qianfeng.goods.service.CartService;
import com.qianfeng.goods.service.GoodsService;
import com.qianfeng.goods.service.OrderService;
import com.qianfeng.goods.service.TypeService;
import com.qianfeng.user.bean.Address;
import com.qianfeng.user.bean.User;
import com.qianfeng.user.service.AddressService;
import com.qianfeng.utils.Message;
import com.qianfeng.utils.Pagetion;
import com.qianfeng.utils.Response;

@Controller
@RequestMapping("/goods/")
public class GoodsController {
	private CartService cartService = new CartService();
	private OrderService orderService = new OrderService();
	private GoodsService goodsService = new GoodsService();
	private TypeService typeService = new TypeService();
	private AddressService addressService = new AddressService();

	@ResponseBody
	@RequestMapping("addToCart")
	public Response addToCart(HttpSession session, Cart cart) {
		User user = (User) session.getAttribute("user");

		Response rs = new Response();
		if (user != null) {
			cart.setUser_id(user.getId());
			boolean flg = cartService.addToCart(cart);
			if (flg) {
				rs.setCode(1);
				rs.setMessage("恭喜您,商品已经成功添加到购物车");
			} else {
				rs.setCode(-1);
				rs.setMessage("对不起,添加失败,要不您再试试...");
			}
		} else {
			rs.setCode(Message.LOUT_CODE);
		}

		return rs;
	}

	@ResponseBody
	@RequestMapping("deleteCart")
	public Response deleteCart(HttpSession session, Cart cart) {
		User user = (User) session.getAttribute("user");

		Response rs = new Response();
		if (user != null) {
			cart.setUser_id(user.getId());
			boolean flg = cartService.deleteByIdAndUserId(cart);
			if (flg) {
				rs.setCode(1);
			} else {
				rs.setCode(-1);
				rs.setMessage("对不起,删除失败");
			}
		} else {
			rs.setCode(Message.LOUT_CODE);
		}

		return rs;
	}

	@RequestMapping("pay")
	public String pay(HttpSession session, Order order) {
		User user = (User) session.getAttribute("user");

		if (user != null) {

			order.setUser_id(user.getId());

			boolean flg = orderService.updateOrderStatus(order);
			if (flg) {
				return "redirect:/goods/toOrderPage";
			} else {

				return "redirect:/goods/toCartPage";
			}
		} else {
			return "login";
		}
	}

	@RequestMapping("toCartPage")
	public String toCartPage(HttpSession session, Cart cart, HttpServletRequest req) {
		// 判断登录
		User user = (User) session.getAttribute("user");
		if (user != null) {

			cart.setUser_id(user.getId());

			List<Cart> list = cartService.getList(cart);
			req.setAttribute("carts", list);
			return "cart";
		} else {
			return "login";
		}

	}

	@RequestMapping("toGoodsInfoPage")
	public String toGoodsInfoPage(Goods goods, HttpServletRequest req) {

		Goods goods2 = goodsService.getById(goods.getId());
		System.out.println(goods2);
		req.setAttribute("goods2", goods2);
		return "goodsInfo";
	}

	@RequestMapping("toMainPage")
	public String toMainPage(HttpServletRequest req, Goods goods) {
		List<TypeBean> typeList = typeService.getListAll();
		req.setAttribute("typeList", typeList);

		// 拼接分页

		int count = goodsService.getCount(goods);

		String paging = Pagetion.paging(goods, count, "goods/toMainPage", req.getParameterMap());

		req.setAttribute("paging", paging);

		// 获取商品
		List<Goods> list = goodsService.getList(goods);
		req.setAttribute("goodsList", list);

		req.setAttribute("goods", goods);
		String orderBy = JSONArray.toJSONString(goods.getOrderBy());
		System.out.println(orderBy);
		req.setAttribute("orderBy", orderBy);

		return "main";
	}

	@RequestMapping("toOrder")
	public String toOrder(HttpSession session, Order order, HttpServletRequest req) {
		User user = (User) session.getAttribute("user");

		if (user != null) {

			order.setUser_id(user.getId());

			long order_id = orderService.saveOrder(order);
			if (order_id > 0) {
				req.setAttribute("order_id", order_id);
				return "pay";
			} else {
				return "redirect:/goods/toCartPage";
			}
		} else {
			return "login";
		}
	}

	@RequestMapping("toOrderPage")
	public String toOrderPage(HttpSession session, Order order, HttpServletRequest req) {
		User user = (User) session.getAttribute("user");
		if (user != null) {
			order.setUser_id(user.getId());

			int count = orderService.getCount(order);

			String paging = Pagetion.paging(order, count, "goods/toOrderPage", req.getParameterMap());

			List<Order> list = orderService.getList(order);

			req.setAttribute("paging", paging);
			req.setAttribute("list", list);

			return "orders";
		} else {
			return "login";
		}
	}

	@RequestMapping("toToOrderPage")
	public String toToOrderPage(HttpSession session, Cart cart, HttpServletRequest req) {
		User user = (User) session.getAttribute("user");

		if (user != null) {

			List<Address> addresses = addressService.getAddressesByUserId(user.getId());
			req.setAttribute("addresses", addresses);

			cart.setUser_id(user.getId());
			List<Cart> list = cartService.getGoodsByCartId(cart);

			req.setAttribute("carts", list);

			return "toOrder";
		} else {
			return "login";
		}
	}

	@ResponseBody
	@RequestMapping("updateCart")
	public Response updateCart(HttpSession session, Cart cart) {
		User user = (User) session.getAttribute("user");
		Response rs = new Response();
		if (user != null) {
			cart.setUser_id(user.getId());

			boolean flg = cartService.updateCartByIdAndUserId(cart);
			if (flg) {
				rs.setCode(1);
			} else {
				rs.setCode(-1);
				rs.setMessage("对不起,修改数量失败");
			}

		} else {
			rs.setCode(Message.LOUT_CODE);
		}
		return rs;

	}

}
